<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'main';
if($_RQ['op']=='main'){
	//这里整理跳转到游戏页面
	$id=intval($_RQ['id']);
	if($id){
		$game=pdo_get('jy_weishop_game',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$game=pdo_get('jy_weishop_game',array('status'=>1,'uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_red_rain'));	
	}
	$game['pluginset']=$game['pluginset']?json_decode($game['pluginset'],true):array('rewardtype'=>0);
	if($game['merchid']){
		$sender=pdo_get('jy_merch_user',array('id'=>$game['merchid'],'uniacid'=>$_SESSION['uniacid']), array('merchname','logo','desc'));
		$sender['nickname']=$sender['merchname'];
		$sender['avatar']=$sender['logo'];
	}elseif($game['mid']){
		$sender=pdo_get('core_members',array('id'=>$game['mid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));
	}else{
		$sender=array(
			'nickname'=>$_SESSION['account']['title'],
			'avatar'=>$_SESSION['account']['logo'],
		);
	}
	$game['lastnum']=model('jy_weishop/game')->check_num($id);
	$game['canyu_num']=model('jy_weishop/game')->canyu_num($id);
	$logs=model('jy_weishop/game')->get_log($id);
	$marqueetip='';
	foreach($logs as $lv){
		$nickname=pdo_getcolumn('core_members',array('id'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']), 'nickname');
		$time=date('Y-m-d H:i:s',$lv['createtime']);
		$lv['reward']=json_decode($lv['reward'],true);
		$reward_name=$lv['reward']['title'];
		$marqueetip .=$nickname.'[抽到《'.$reward_name.'》,时间:'.$time.']；';
	}
	$game['marqueetip']=$marqueetip;
	$game['reward']=json_decode($game['reward'],true);
	foreach($game['reward'] as &$av){
		$av['image']=tomedia($av['image']);
	}
	$return['game']=$game;
	$return['sender']=$sender;
	$return['list']=array();
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','mobile','avatar','nickname'));
	$member['id']=$_SESSION['uid'];
	$return['member']=$member;
	$return['sharedata']=array(
		'title'=> '来红包雨抽奖啦！！！',
		'desc'=> $game['title'],
		'link'=>custom_url('jy_red_rain/index//id='.$id.'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=> tomedia('../addons/jy_red_rain/images/red_rain.png'),
	);
	set_html('title','红包雨');
	exi($return);
}
if($_RQ['op']=='getredlasttime'){
	
	exi($return);
}
if($_RQ['op']=='getlog'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_game_getlog',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
			$v['createtime']=date('Y-m-d H:m:s',$v['createtime']);
			$v['reward']=json_decode($v['reward'],true);
			$v['typetitle']=$this->prizeType[$v['reward']['type']];
			$v['fee']=$v['fee']>0?$v['fee']:$v['reward']['jifen'];
			if($v['reward']['type']==1){
				$v['fee']=$v['reward']['title'];
			}
			$v['member']=cfc('core')->member_show($v['mid']);
	}
	exi($return);
}
if($_RQ['op']=='getred'){
	$return['pluginset']['red_min']=$return['pluginset']['red_min']?:1;
	$return['pluginset']['red_max']=$return['pluginset']['red_max']?:3;
	$money=rand($return['pluginset']['red_min'],$return['pluginset']['red_max']);
	$memberid=$_RQ['memberid'];
	$log_id=model('core/member')->add_credit1($_SESSION['uid'],$money,$remark='排号红包雨',$from_plugin='jy_red_rain',$tag='queue');
	pdo_update('jy_weishop_queue_member',array('getredtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$memberid));
	$redlasttime=intval($return['pluginset']['red_time'])?:5;
	$redlasttime=$redlasttime*60;
	$return['money']=$money;
	$return['redlasttime']=model('jy_weishop/model')->date($redlasttime);
	exi($return);
}
if($_RQ['op']=='getreward'){
	$post=get_postdata();	
	$game_id=intval($post['id']);
	$check_num=model('jy_weishop/game')->check_num($game_id);
	if($check_num<=0){
		exi('您已经没有抽奖次数了！','error');
	}
	if ($game_id) {
		$game=pdo_get('jy_weishop_game',array('id'=>$game_id,'uniacid'=>$_SESSION['uniacid']));
		$game['pluginset']=json_decode($game['pluginset'],true);
	}
	$rewardtype=array('积分','余额','会员红包','现金红包');
	$waitid=pdo_getcolumn('jy_weishop_game_wait',array('game_id'=>$game_id,'mid'=>$_SESSION['uid'],'status'=>0),'id');
	$pluginset=$game['pluginset'];
	$red_max=$game['pluginset']['red_max']?:0;
	$red_min=$game['pluginset']['red_min']?:0;
	$fee=mt_rand($red_min*100,$red_max*100);
	$fee=$fee/100;
	$info='恭喜您获得'.$fee.$rewardtype[$pluginset['rewardtype']];
	if($pluginset['rewardtype']==0){
		$reward=array(
			'title'=>'红包雨奖励积分',
			'fee'=>$fee,
		);
		if($fee>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit1',$fee,1,'jy_red_rain','红包雨奖励');
		}
	}elseif($pluginset['rewardtype']==1){
		$reward=array(
			'title'=>'红包雨奖励余额',
			'fee'=>$fee,
		);
		if($fee>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit2',$fee,1,'jy_red_rain','红包雨奖励');
		}
	}elseif($pluginset['rewardtype']==2){
		$reward=array(
			'title'=>'红包雨奖励会员红包',
			'fee'=>$fee,
		);
		if($fee>0){
			cfc('core')->member_credit($_SESSION['uid'],'credit2',$fee,1,'jy_red_rain','红包雨奖励');
		}
	}elseif($pluginset['rewardtype']==1){
		$reward=array(
			'title'=>'红包雨奖励现金红包',
			'fee'=>$fee,
		);
		if($fee>0){
			$params=array(
				'mid'=>$_SESSION['uid'],
				'pidcode'=>$game_id.'_'.$waitid,
				'plugin'=>'jy_red_rain',
				'money'=>$fee,
				'remark'=>'红包雨奖励',
			);
			cfc('red_pack')->get_money($params);
		}
	}
	$log_data = array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'game_id'=>$game['id'],
			'reward'=>ijson_encode($reward),
			'rewardkey'=>$pluginset['rewardtype'],
			'fee'=>$fee,
			'status'=>0,
			'createtime'=>TIMESTAMP
	);
	pdo_insert('jy_weishop_game_getlog', $log_data);
	$logid=pdo_insertid();
	pdo_update('jy_weishop_game_wait',array('status'=>1),array('id'=>$waitid,'mid'=>$_SESSION['uid']));
	$return = array(
			'status' =>1,
			'id' => 0,
			'info' => $info,
			'fee' => $fee,
			'logid' => $logid
	);
	exi($return);
}
if($_RQ['op']=='reward'){
	$post=get_postdata();	
	$id=intval($post['id']);
	$reward_id=intval($post['reward_id']);
	$log_id=intval($post['log_id']);
	$res=model('jy_weishop/game')->get_reward($id,$reward_id,$log_id);
	exi($res);
}
if($_RQ['op']=='set_reward'){
	$post=get_postdata();	
	$id=intval($post['id']);
	$log=pdo_get('jy_weishop_game_getlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$reward=json_decode($log['reward'],true);
	$game=pdo_get('jy_weishop_game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['game_id']));
	$res=model('jy_weishop/game')->set_reward($reward,$log['mid'],$game,$id,true);
	exi('领取成功！');
}